package com.jvmoutofmemory.octopus.ui;

import com.jvmoutofmemory.octopus.util.PrintUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;

public class OctopusMenu extends JMenuBar {

    private JMenu operation;
    private JMenuItem selectFolder;
    private JMenuItem clear;
    private JMenuItem startPrint;
    private File lastSelectFolder;

    private JMenu setting;
    private JMenuItem printInterval;
    private Integer printIntervalSecond = 5;

    private MainOctopusFrame mainOctopusFrame;

    public OctopusMenu(MainOctopusFrame mainOctopusFrame) {
        this.mainOctopusFrame = mainOctopusFrame;
    }

    public void initialize() {
        operation = new JMenu("操作");
        selectFolder = new JMenuItem("选择文件夹");
        clear = new JMenuItem("清空文件列表");
        startPrint = new JMenuItem("打印所有文件");
        operation.add(selectFolder);
        operation.add(clear);
        operation.add(startPrint);

        this.add(operation);

        setting = new JMenu("设置");
        printInterval = new JMenuItem("打印间隔");
        setting.add(printInterval);
        this.add(setting);

        selectFolder.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (mainOctopusFrame.getOctopusTableModel().hasUnprintedFiles()
                        && JOptionPane.showConfirmDialog(mainOctopusFrame, "列表中存在尚未打印的文件, 确定选择新文件夹吗?", "确认操作", JOptionPane.YES_NO_OPTION)
                        != JOptionPane.YES_OPTION) {
                    return;
                }

                JFileChooser chooser = new JFileChooser(lastSelectFolder);
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (chooser.showOpenDialog(mainOctopusFrame) == JFileChooser.APPROVE_OPTION) {
                    File directory = chooser.getSelectedFile();
                    lastSelectFolder = directory;
                    File[] files = directory.listFiles(new FilenameFilter() {
                        @Override
                        public boolean accept(File dir, String name) {
                            return name.endsWith(".pdf");
                        }
                    });

                    if (files == null || files.length == 0) {
                        JOptionPane.showMessageDialog(mainOctopusFrame, "您选择的文件夹没有pdf文件, 请重新选择!");
                        return;
                    }

                    mainOctopusFrame.getOctopusTableModel().clearAllFiles();
                    for (File file : files) {
                        mainOctopusFrame.getOctopusTableModel().addFile(file);
                    }

                    mainOctopusFrame.renderTableData();
                }
            }
        });

        clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mainOctopusFrame.getOctopusTableModel().clearAllFiles();
                mainOctopusFrame.renderTableData();
            }
        });

        startPrint.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                PrintUtil.printAll(mainOctopusFrame, mainOctopusFrame.getOctopusTableModel().getFileModels(), printIntervalSecond);
            }
        });

        printInterval.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String value = JOptionPane.showInputDialog(mainOctopusFrame,"请输入打印间隔(秒)", printIntervalSecond);
                if (value != null && !"".equals(value)) {
                    printIntervalSecond = Integer.valueOf(value);
                }
            }
        });
    }

    public Integer getPrintIntervalSecond() {
        return printIntervalSecond;
    }
}
